﻿using Dapper;
using DevExpress.DataAccess.Native.Json;
using DevExpress.XtraReports.Native;
using Microsoft.OData.Edm;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Services;
using YAKEJI.Abp.Database;
using YAPACS.Organizations;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Interface.Imitate;

namespace YAPACS.RisServer.Services
{
    public class HomeCardService(ICommonUse commonUse, ICurrentUserOrganization currentUserOrg) : DomainService, IHomeCardService, ITransientDependency
    {
        private readonly ICommonUse _commonUse = commonUse;
        private readonly ICurrentUserOrganization _currentUserOrg = currentUserOrg;
        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public List<object> GetHomeCardData()
        {
            DatabaseType databaseType;
            using (var connection = _commonUse.GetDapperConnection(out databaseType))
            {
                var parameters = new DynamicParameters();
                parameters.Add("@DepartmentId", _currentUserOrg.DepartmentId.Value);
                parameters.Add("@startTime", Clock.Now.Date);
                parameters.Add("@endTime", Clock.Now.AddDays(1).Date);
                string sqlStr = $@"
									select 'register' as category ,count(*) as total 
									from exam  
									where 
										(RegisterTime between  @startTime and @endTime)
										and 
										Department = @DepartmentId
	
									union 
									
								    select 'appointed' as category, count(*) as total
									from exam 
									where 
										(AppointmentTime between  @startTime and @endTime)
										and 
										(RegisterTime not between @startTime and @endTime)
										and 
										Department = @DepartmentId

									union

									select 'appointAnotherDay' as  category,count(*) as total
									from exam 
									where 
										(AppointmentTime not between  @startTime and @endTime)
										and 
										(RegisterTime between  @startTime and @endTime)
										and
										Department = @DepartmentId
	
									union

									select 'exam' as category , count(*) as total
									from exam 
									where 
									   (ExamTime between  @startTime and @endTime)
									   and 
										Department = @DepartmentId
   
									union

									select 'reported' as category ,count(*) as total
									from report 
									where 
										(ReportTime between  @startTime and @endTime)
										and 
											Exam 
												in
											(select oid from exam where Department = @DepartmentId)
	
									union

									select 'completed' as category ,count(*) as total
									from exam 
									where
										(ExamTime between  @startTime and @endTime)
										and 
									    ExamStatus =7
										and 
										Department = @DepartmentId
									";

                var sqlResult = connection.Query(sqlStr, parameters).ToList();
                List<object> items = [];

                Dictionary<string, object> item1 = new Dictionary<string, object>();
                item1.Add("category", "登记");
                item1.Add("total", (int)sqlResult[0].total - (int)sqlResult[2].total);
                items.Add(item1);


                Dictionary<string, object> item2 = new Dictionary<string, object>();
                item2.Add("category", "预约");
                item2.Add("total", sqlResult[1].total);
                items.Add(item2);

			    Dictionary<string,object> item3 = new Dictionary<string, object>();
				item3.Add("category", "检查");
				item3.Add("total", sqlResult[3].total);
				items.Add(item3);

				Dictionary<string,object> item4= new Dictionary<string, object>();
				item4.Add("category", "报告");
				item4.Add("total", sqlResult[4].total);
				items.Add(item4);

				Dictionary<string,object> item5= new Dictionary<string, object>();
				item5.Add("category", "完成");
				item5.Add("total", sqlResult[5].total);
				items.Add(item5);

                return items;
            }
        }
    }
}
